import 'dart:async';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hong_vpn_new_20240626/ui/home/home.dart';
import 'package:hong_vpn_new_20240626/utils/ex/map_ex.dart';
import 'package:ipapi/ipapi.dart';

import '../utils/utils.dart';
import '../utils/zz_navigator.dart';

class LoadingWidget extends StatefulWidget {
  const LoadingWidget({super.key});

  @override
  State<LoadingWidget> createState() => _LoadingWidgetState();
}

class _LoadingWidgetState extends State<LoadingWidget> {
  double _progress = 0;
  @override
  void initState() {
    super.initState();
    _initConfigs();
  }

  resolveIP() async {
    var result = await IpApi.getData();

    zzLog("IpApi.getData = ${result?.toJson().toJsonStr()}");
    if (result?.countryCode == "CN" && mounted) {
      zzLog("message");

      await showDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: const Text("Prompt"),
            content: const Text(
                "Sorry, the local laws of the region where the app is located have content restrictions"),
            actions: [
              TextButton(
                  onPressed: () {
                    exit(0);
                  },
                  child: const Text("I know"))
            ],
          );
        },
      );
    }
  }

  _initConfigs() async {
    if (!kDebugMode) {
      await resolveIP();
    }
    await Utils.initConfig(
      progress: (p0) {
        _progress = p0;
        setState(() {});
      },
    );
    Timer.periodic(const Duration(milliseconds: 1), (timer) {
      _progress += 0.001;
      setState(() {});
      if (_progress >= 1) {
        timer.cancel();
        _jumpToMain();
      }
    });
  }

  _jumpToMain() {
    ZZNavigator.presentAndRemoveUntil(
        widget: const HomeWidget(), animation: false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Image.asset(
            "assets/images/加载页面.png",
            width: double.infinity,
            height: double.infinity,
            fit: BoxFit.cover,
          ),
          Container(
            padding: EdgeInsets.only(
                top: ScreenUtil().screenHeight * 0.25,
                bottom: ScreenUtil().screenHeight * 0.2),
            margin: MediaQuery.of(context).padding,
            width: double.infinity,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Image.asset("assets/images/icon 2.png", width: 80.spMin),
                const Spacer(),
                Padding(
                  padding: const EdgeInsets.only(top: 80, left: 50, right: 50),
                  child: LinearProgressIndicator(
                    value: _progress,
                    color: Colors.grey.shade400,
                    backgroundColor: Colors.black.withOpacity(0.4),
                    minHeight: 8,
                    borderRadius: const BorderRadius.all(Radius.circular(5)),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}
